package yja.gzw.app.adapter

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import yja.gzw.app.R
import yja.gzw.app.activity.DuntaixiangqingActivity
import yja.gzw.app.activity.TopshowActivity
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.Commenthuifu
import yja.gzw.app.model.bean.Commentpl
import yja.gzw.app.model.bean.QueryItem
import yja.gzw.app.viewmodel.Mainviewmodel
import com.google.android.material.bottomsheet.BottomSheetDialog
import yja.gzw.app.activity.JubaoActivity


class PinglongAdapter (private val context: Context, private val list: List<Commentpl>,private val listener: OnItemClickListener): RecyclerView.Adapter<RecyclerView.ViewHolder>(),HuifuPinglongAdapter.OnItemClickListener{

    private lateinit var bottomSheetDialog: BottomSheetDialog
    interface OnItemClickListener {
        fun onItemClick(position: Int, uri: Commentpl)
    }

   private lateinit var fabiada: HuifuPinglongAdapter
   private val viewmodel: Mainviewmodel=Mainviewmodel()
    private var view: View?=null
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        view= LayoutInflater.from(parent.context).inflate(R.layout.item_view_plinglong,parent,false)
        Log.e("图片地址展示","走到了2")
        return ViewHodeView(view!!)
    }

    override fun getItemCount(): Int {
       return  list.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(holder is ViewHodeView){
            val requestOptions = RequestOptions.bitmapTransform(CircleCrop())
            Glide.with(context).load("http://117ai.32.hzgqapp.com/"+list[position].user.avatar)
                .apply(requestOptions)
                .into(holder.touxiang)
            holder.nicheng.text=list[position].user.nickname
            holder.neirong.text=list[position].content
            holder.jubao.setOnClickListener {

                bottomSheetDialog = BottomSheetDialog(context)
                val view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_jubao, null)
                bottomSheetDialog.setContentView(view)
                val chacha =view.findViewById<ImageView>(R.id.chacha)
                val jubaoh =view.findViewById<ImageView>(R.id.jubaohong)
                chacha.setOnClickListener {
                    bottomSheetDialog.dismiss()

                }
                jubaoh.setOnClickListener {
                    val inten=Intent(context, JubaoActivity::class.java)
                context.startActivity(inten)

                }




                bottomSheetDialog.show()


            }


            if(list[position].is_fav_count==true){
                holder.dianzan.setImageResource(R.mipmap.dianzanhou)
            }else{
                holder.dianzan.setImageResource(R.mipmap.dianzan)
            }
            holder.dianzanliang.text = (list[position].fav_count).toString()

            holder.dianzan.setOnClickListener {
                listener.onItemClick(position,list[position])
//                Log.e("评论点赞数字",list[position].fav_count.toString())
//                holder.dianzanliang.text = (list[position].fav_count+1).toString()
//                viewmodel.ondianzanplwang(UserData.token,mapOf("id" to list[position].id.toString()))
//
//                viewmodel.ondianzanpl.observe(context as DuntaixiangqingActivity, Observer{
//                    if(it.code==1){
//                        Log.e("评论点赞数字状态",it.data.is_fav_count.toString())
//
//                        if(it.data.is_fav_count.toString()=="1"){
//                            Log.e("评论点赞数字状态","成功")
//                            holder.dianzan.setImageResource(R.mipmap.dianzanhou)
//                            holder.dianzanliang.text = (list[position].fav_count).toString()
//                        }else{
//                            Log.e("评论点赞数字状态","失败")
//                            holder.dianzanliang.text = (list[position].fav_count-1).toString()
//                            holder.dianzan.setImageResource(R.mipmap.dianzan)
//                        }
//
//
//                    }else{
//                        Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
//
//
//                    }
//
//                })



            }



            holder.huifu.setOnClickListener {
                bottomSheetDialog = BottomSheetDialog(context)
                val view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_fasongpinglun, null)
                bottomSheetDialog.setContentView(view)
                val shurukuang =view.findViewById<TextView>(R.id.shurukuang)
                val queding =view.findViewById<TextView>(R.id.queding)
                val quxiao =view.findViewById<TextView>(R.id.quxiao)
                val chacha =view.findViewById<ImageView>(R.id.chacha)
                chacha.setOnClickListener {
                    bottomSheetDialog.dismiss()
                }
                quxiao.setOnClickListener {
                    bottomSheetDialog.dismiss()
                }
                queding.setOnClickListener {
                   // Toast.makeText(context,shurukuang.text.toString(), Toast.LENGTH_SHORT).show()
                    viewmodel.onfabupinglongwang(UserData.token, mapOf("forum_id" to list[position].forum_id.toString(),"pid" to list[position].id.toString(),"content" to shurukuang.text.toString(),"level" to "2","reply_id" to list[position].reply_id.toString()))
                    viewmodel.onfabupinglong.observe(context as DuntaixiangqingActivity, Observer{
                        if(it.code==1){
                            bottomSheetDialog.dismiss()
                            Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
                        }else{
                            Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
                        }

                    })



                }

                bottomSheetDialog.show()

            }


            holder.zhankai.setOnClickListener {
                viewmodel.onhuifuwang(UserData.token, mapOf("pid" to list[position].id.toString()))
                viewmodel.onhuifu.observe(context as DuntaixiangqingActivity, Observer{
                    if(it.code==1){
                        holder.xplreview.layoutManager=
                LinearLayoutManager(context)
            fabiada= HuifuPinglongAdapter(context,it.data.data,this)
            holder.xplreview.adapter= fabiada
                    }
                })
                holder.xplreview.visibility=View.VISIBLE
                holder.shouqi.visibility=View.VISIBLE
//                if(holder.xplreview.isVisible){
//                    holder.xplreview.visibility=View.GONE
//                }else{
//                    holder.xplreview.visibility=View.VISIBLE
//
//                }


            }

            holder.shouqi.setOnClickListener {
                holder.xplreview.visibility=View.GONE

                holder.shouqi.visibility=View.GONE
            }


        }


    }

    inner class ViewHodeView(itemView: View):RecyclerView.ViewHolder(itemView){

        var touxiang: ImageView =itemView.findViewById(R.id.touxiang)
        var nicheng: TextView =itemView.findViewById(R.id.nicheng)
        var neirong: TextView =itemView.findViewById(R.id.neirong)
        var zhankai: TextView =itemView.findViewById(R.id.zhankai)
        var xplreview: RecyclerView =itemView.findViewById(R.id.xplreview)
        var huifu: TextView =itemView.findViewById(R.id.huifu)
        var dianzan: ImageView =itemView.findViewById(R.id.dianzan)
        var dianzanliang: TextView =itemView.findViewById(R.id.dianzanliang)
        var jubao: ImageView =itemView.findViewById(R.id.jubao)
        val shouqi:TextView =itemView.findViewById(R.id.shouqi)



    }

    override fun onItemClick(position: Int, uri: Commenthuifu) {
        bottomSheetDialog = BottomSheetDialog(context)
        val view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_fasongpinglun, null)
        bottomSheetDialog.setContentView(view)
        val shurukuang =view.findViewById<TextView>(R.id.shurukuang)
        val queding =view.findViewById<TextView>(R.id.queding)
        val quxiao =view.findViewById<TextView>(R.id.quxiao)
        val chacha =view.findViewById<ImageView>(R.id.chacha)
        chacha.setOnClickListener {
            bottomSheetDialog.dismiss()
        }
        quxiao.setOnClickListener {
            bottomSheetDialog.dismiss()
        }
        queding.setOnClickListener {
            // Toast.makeText(context,shurukuang.text.toString(), Toast.LENGTH_SHORT).show()
            viewmodel.onfabupinglongwang(UserData.token, mapOf("forum_id" to list[position].forum_id.toString(),"pid" to list[position].id.toString(),"content" to shurukuang.text.toString(),"level" to "2","reply_id" to list[position].reply_id.toString()))
            viewmodel.onfabupinglong.observe(context as DuntaixiangqingActivity, Observer{
                if(it.code==1){

                    viewmodel.onhuifuwang(UserData.token, mapOf("pid" to list[position].id.toString()))
                    viewmodel.onhuifu.observe(context as DuntaixiangqingActivity, Observer{
                        if(it.code==1){
                            context.findViewById<RecyclerView>(R.id.xplreview).layoutManager=
                                LinearLayoutManager(context)
                            fabiada= HuifuPinglongAdapter(context,it.data.data,this)
                            context.findViewById<RecyclerView>(R.id.xplreview).adapter= fabiada
                        }
                    })



                    Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
                }else{
                    Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
                }

            })



        }

        bottomSheetDialog.show()
    }

}